package com.itheima.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.Role;
import com.itheima.domain.User;
import com.itheima.factory.DaoInstanceFactory;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Util;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao dao = DaoInstanceFactory.getMapper(UserDao.class);

    /**
     * 分页查询的方法
     *
     * @param pageSizeStr
     * @param currentPageStr
     * @return
     */
    @Override
    public PageInfo<User> findByPage(String pageSizeStr, String currentPageStr) {
        //设置初始分页查询的页数
        Integer currentPage = 1;
        try {
            currentPage = Integer.parseInt(currentPageStr);
        } catch (NumberFormatException e) {
            // e.printStackTrace();
        }
        Integer pageSize = 5;
        try {
            pageSize = Integer.parseInt(pageSizeStr);
        } catch (NumberFormatException e) {
            // e.printStackTrace();
        }
        //设置分页
        PageHelper.startPage(currentPage, pageSize);
        //是用daofindAll方法进行查询
        List<User> users = dao.findAll();
        //定义一个集合,对上一个集合的user成员进行比遍历,如果status为1,则给status赋值为开启,否则赋值为关闭
        /*List<User> users1 = new ArrayList<>();
        for (User user : users) {
            int status = user.getStatus();
            if (status==1){
                user.setStatusStr("开启");
            }else if (status==0){
                user.setStatusStr("关闭");
            }
            users1.add(user);
        }*/

        PageInfo<User> info = new PageInfo<>(users);
        return info;

    }

    @Override
    public void addUser(User user) {
        /* user.setId(UUID.randomUUID().toString().replace("-", ""));*/
        user.setId(String.valueOf(System.currentTimeMillis()));
        //密码进行加密处理
        String s = MD5Util.md5(user.getPassword());
        user.setPassword(s);
        System.out.println(user);
        dao.addUser(user);
    }

    @Override
    public void deleteUser(String[] ids) {
        dao.deleteUser(ids);
        //System.out.println(Arrays.toString(ids));
    }

    @Override
    public PageInfo<User> search(String username, String pageSizeStr, String currentPageStr) {
        Integer currentPage = 1;
        try {
            currentPage = Integer.parseInt(currentPageStr);
        } catch (NumberFormatException e) {
        }
        Integer pageSize = 5;
        try {
            pageSize = Integer.parseInt(pageSizeStr);
        } catch (NumberFormatException e) {
        }
        PageHelper.startPage(currentPage, pageSize);
        List<User> users = dao.search(username);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public User findById(String id) {
        return dao.findById(id);
    }

    @Override
    public void updateStatus(String id, int status) {
        UserDao dao = DaoInstanceFactory.getMapper(UserDao.class);
        dao.updateStatus(id, status);
    }

    @Override
    public List<Role> findAllByUserId(String id) {
        UserDao dao = DaoInstanceFactory.getMapper(UserDao.class);
        return dao.findAllRoleByUserId(id);
    }

    @Override
    public void updateRole(String userId, String[] ids) {
        UserDao dao = DaoInstanceFactory.getMapper(UserDao.class);
        //先将所有的角色删除
        dao.deleteRole(userId);

        for (String id : ids) {
            UserDao dao1 = DaoInstanceFactory.getMapper(UserDao.class);
            dao1.updateRole(userId, id);
        }
    }

    @Override
    public List<Role> findRoleByUserId(String id) {
        UserDao dao = DaoInstanceFactory.getMapper(UserDao.class);
        return dao.findRoleByUserId(id);
    }

}
